﻿<UserControl 
    x:Class="ViewSwitchingNavigation.Email.Views.InboxView"
    x:Name="userControl"
    xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"  
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:infrastructure="clr-namespace:ViewSwitchingNavigation.Infrastructure;assembly=ViewSwitchingNavigation.Infrastructure" 
    xmlns:GuidanceTools="clr-namespace:Microsoft.Practices.Prism.GuidanceTools;assembly=Microsoft.Practices.Prism.GuidanceTools" 
    mc:Ignorable="d"
    d:DesignHeight="500" d:DesignWidth="400" 
    d:DataContext="{d:DesignData /SampleData/InboxViewModelSampleData.xaml}">

    <UserControl.Resources>
        <infrastructure:ObjectToVisibilityConverter x:Key="ObjectToVisibility"/>

        <infrastructure:ObservableCommand x:Name="OpenMessageCommand" Value="{Binding OpenMessageCommand}"/>

        <Style x:Key="EmailListContainerStyle" TargetType="ListBoxItem" BasedOn="{StaticResource DefaultListBoxItemStyle}">
            <Setter Property="Padding" Value="0"/>
            <Setter Property="HorizontalContentAlignment" Value="Stretch"/>
        </Style>

        <DataTemplate x:Key="EmailListTemplate">
            <Border BorderBrush="Gray" BorderThickness="0,0,0,1">
                <Grid>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="Auto"/>
                    </Grid.RowDefinitions>
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="*"/>
                        <ColumnDefinition Width="Auto"/>
                    </Grid.ColumnDefinitions>

                    <TextBlock Text="{Binding Subject}" Grid.Row="0"/>
                    <TextBlock Text="{Binding From}" Grid.Row="1"/>

                    <StackPanel Grid.Column="1" Grid.RowSpan="2"  Orientation="Horizontal">
                        <Button 
                            Command="{Binding Value, Source={StaticResource OpenMessageCommand}}" 
                            CommandParameter="{Binding}"
                            Margin="5,0" 
                            HorizontalAlignment="Center" 
                            VerticalAlignment="Center" 
                            Height="Auto" 
                            AutomationProperties.AutomationId="OpenButton">
                            Open
                        </Button>
                        <GuidanceTools:InfoTipToggleButton VerticalAlignment="Center">
                            <StackPanel MinWidth="100" MinHeight="24" MaxWidth="400">
                                <TextBlock Margin="2" TextWrapping="Wrap">The Open button, enabled when an email is selected, inokes the OpenMessageCommand on the InboxViewModel that navigates the region to the EmailView while supplying the email id this email.</TextBlock>
                            </StackPanel>
                        </GuidanceTools:InfoTipToggleButton>
                    </StackPanel>
                </Grid>
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="EmailTemplate">
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="*"/>
                </Grid.RowDefinitions>

                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="Auto"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>

                <TextBlock Text="{Binding From}" FontWeight="Bold" FontSize="13" AutomationProperties.AutomationId="FromDataBlock"/>

                <TextBlock Text="To:" FontWeight="Bold"
                           Grid.Row="1" Grid.Column="0" Margin="0,0,5,0" AutomationProperties.AutomationId="ToBlock"/>
                <TextBlock Text="{Binding To}"                           
                           Grid.Row="1" Grid.Column="1" AutomationProperties.AutomationId="ToBlockData"/>


                <TextBlock Text="Subject:" FontWeight="Bold"
                           Grid.Row="2" Grid.Column="0" AutomationProperties.AutomationId="SubjectBlock"/>
                <TextBlock Text="{Binding Subject}"                           
                           Grid.Row="2" Grid.Column="1" AutomationProperties.AutomationId="SubjectDataBlock"/>

                <ScrollViewer Grid.Row="3" Grid.ColumnSpan="2" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
                              Margin="5, 10, 5, 0" VerticalScrollBarVisibility="Auto">
                    <TextBlock Text="{Binding Text}" TextWrapping="Wrap" AutomationProperties.AutomationId="EmailTextBlock"/>
                </ScrollViewer>
            </Grid>
        </DataTemplate>
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" Background="White">
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <StackPanel HorizontalAlignment="Center" Grid.Row="0" Grid.Column="0" Margin="0, 5, 0, 5" Orientation="Horizontal">
            <Button Margin="2,0" Command="{Binding ComposeMessageCommand}">New</Button>
            <Button Margin="2,0" Command="{Binding ReplyMessageCommand}">Reply</Button>
            <GuidanceTools:InfoTipToggleButton>
                <StackPanel MinWidth="100" MinHeight="24" MaxWidth="400">
                    <TextBlock Margin="2" TextWrapping="Wrap">These buttons' view model demonstrate navigation with and without context.</TextBlock>
                    <TextBlock Margin="2" TextWrapping="Wrap">The New button invokes the ComposeEmailCommand on the InboxViewModel that navigates the region to the ComposeEmailView.</TextBlock>
                    <TextBlock Margin="2" TextWrapping="Wrap">The Reply button, enabled when an email is selected, inokes the ReplyMessageCommand on the InboxViewModel that navigates the region to the ComposeEmailView while supplying the email id of the selected email.</TextBlock>
                </StackPanel>
            </GuidanceTools:InfoTipToggleButton>
        </StackPanel>
        <ListBox x:Name="MessagesListBox"
                 ItemsSource="{Binding Messages}" 
                 ItemTemplate="{StaticResource EmailListTemplate}"
                 ItemContainerStyle="{StaticResource EmailListContainerStyle}"
                 Grid.Row="1" AutomationProperties.AutomationId="MessagesList"/>
        <sdk:GridSplitter Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Top" Height="15" ShowsPreview="True" />
        <ContentControl x:Name="Preview" Content="{Binding Messages.CurrentItem}"
                        Margin="5,15,5,5"
                        ContentTemplate="{StaticResource EmailTemplate}"
                        Grid.Row="2" HorizontalContentAlignment="Stretch" VerticalContentAlignment="Stretch"
                        Visibility="{Binding Messages.CurrentItem, Converter={StaticResource ObjectToVisibility}}">
        </ContentControl>
    </Grid>
</UserControl>
